using System.Collections.Generic;
using System.Collections;
using UnityEngine;
using UnityEditor;
using System.IO;
using System.Text;

public partial class UICodeGenerator 
{
    string GetBindCode(CompInfo item,string oldCode = "")
    {
        string code = "";
        switch (item.type)
        {
            case "Button":
                code = $"\t\t{item.name}.onClick.AddListener(OnClick{item.name});";
                break;
            case "Slider":
                code = $"\t\t{item.name}.onValueChanged.AddListener(OnValue{item.name});";
                break;
            case "ScrollRect":
                code = $"\t\t{item.name}.onValueChanged.AddListener(OnValue{item.name});";
                break;
            case "CircularScrollRect":
                code = $"\t\t{item.name}.Init({item.name}UpdateItem);";
                break;
            case "InputField":
                code = $"\t\t{item.name}.onValueChanged.AddListener(OnValue{item.name});";
                break;
            case "Dropdown":
                code = $"\t\t{item.name}.onValueChanged.AddListener(OnValue{item.name});";
                break;
            case "Toggle":
                code = $"\t\t{item.name}.onValueChanged.AddListener(OnValue{item.name});";
                break;
            default:
                break;
        }
        return code;
    }

    string GetBingdingMethodCode(CompInfo item,string oldCode = "")
    {
        string code = "";
        switch (item.type)
        {
            case "Button":
                if(!oldCode.Contains(GetClickMethodNameCode(item.name)))
                {
                    code  = GenerateClickCode(item.name);
                }else{
                    Debug.Log("old code contain new code");
                }
                break;
            case "Slider":
                if(!oldCode.Contains(GetValueChangeMethodNameCode(item.name, "float")))
                {
                    code = GenerateValueChangeCode(item.name, "float");
                }else{
                    Debug.Log("old code contain new code");
                }
                break;
            case "ScrollRect":
                if(!oldCode.Contains(GetValueChangeMethodNameCode(item.name, "Vector2")))
                {
                    code = GenerateValueChangeCode(item.name, "Vector2");
                }else{
                    Debug.Log("old code contain new code");
                }
                break;
            case "CircularScrollRect":
                if(!oldCode.Contains($"\tpublic void {item.name}UpdateItem(GameObject go, int index)"))
                {
                    code = GenerateCircularScrollRect(item.name);
                }else{
                    Debug.Log("old code contain new code");
                }
                break;
            case "InputField":
                if(!oldCode.Contains(GetValueChangeMethodNameCode(item.name, "string")))
                {
                    code = GenerateValueChangeCode(item.name, "string");
                }else{
                    Debug.Log("old code contain new code");
                }
                break;
            case "Dropdown":
                if(!oldCode.Contains(GetValueChangeMethodNameCode(item.name, "int")))
                {
                    code = GenerateValueChangeCode(item.name, "int");
                }else{
                    Debug.Log("old code contain new code");
                }
                break;
            case "Toggle":
                if(!oldCode.Contains(GetValueChangeMethodNameCode(item.name, "bool")))
                {
                    code = GenerateValueChangeCode(item.name, "bool");
                }else{
                    Debug.Log("old code contain new code");
                }
                break;
            default:
                break;
        }
        return code;
    }

    string GenerateClickCode(string name)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine($"\t//{name}的点击函数");
        sb.AppendLine("\tpublic void OnClick" + name + "()");
        sb.AppendLine("\t{");
        sb.AppendLine("\t\t//TODO");
        sb.AppendLine("\t}");
        sb.AppendLine("");
        return sb.ToString();
    }

    string GetClickMethodNameCode(string name)
    {
        return $"\tpublic void OnClick{name}()";
    }

    string GenerateValueChangeCode(string name,string type)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine($"\t//{name}的数值变化函数");
        sb.AppendLine($"\tpublic void OnValue{name}({type} value)");
        sb.AppendLine("\t{");
        sb.AppendLine("\t\t//TODO");
        sb.AppendLine("\t}");
        sb.AppendLine("");
        return sb.ToString();
    }

    string GetValueChangeMethodNameCode(string name,string type)
    {
        if(type == "")
        {
            Debug.LogError("type is empty");
            return null;
        }
        string method = $"\tpublic void OnValue{name}({type} value)";
        return method;
    }

    string GenerateCircularScrollRect(string name)
    {
        StringBuilder sb = new StringBuilder();
        sb.AppendLine($"\t//{name}的更新函数");
        sb.AppendLine($"\tpublic void {name}UpdateItem(GameObject go, int index)");
        sb.AppendLine("\t{");
        sb.AppendLine("\t\t//TODO");
        sb.AppendLine("\t}");
        sb.AppendLine("");
        return sb.ToString();
    }

}